<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form size="small" :model="inputForm" ref="inputForm" :class="method==='view'?'readonly':''"  :disabled="method==='view'" @keyup.enter.native="doSubmit()"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="加分原因" prop="addReason"
                :rules="[
                  {required: true, message:'加分原因不能为空', trigger:'blur'}
                 ]">
              <el-select style="width: 100%" v-model="inputForm.addReason"  placeholder="请填写加分原因"  >
                <el-option v-for="addReason in $dictUtils.getDictList('evaluation_reason')" :label="addReason.label" :value="addReason.label" :key="addReason.value"></el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="加分时间" prop="addTime"
                :rules="[
                  {required: true, message:'加分时间不能为空', trigger:'blur'}
                 ]">
                <el-date-picker
                      v-model="inputForm.addTime"
                      type="datetime"
                      style="width: 100%;"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="加分数" prop="addPoint"
                :rules="[
                  {required: true, message:'加分数不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.addPoint" placeholder="请填写加分数"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="审核状态" prop="addState"
                :rules="[
                  {required: true, message:'审核状态不能为空', trigger:'blur'}
                 ]">
              <el-select style="width: 100%" v-model="inputForm.addState"  placeholder="请填写审核状态"  >
                <el-option v-for="addState in $dictUtils.getDictList('audit_status')" :label="addState.label" :value="addState.value" :key="addState.value"></el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="审批人" prop="approver">
              <el-input v-model="inputForm.approver" placeholder="请填写审批人"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="附件" prop="addEnclosure">
              <el-upload
                list-type="picture-card"
                :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/moredumain/studentMoreduMain`"
                :headers="{token: $cookie.get('token')}"
                :on-preview="(file, fileList) => {
                    $alert(`<img style='width:100%' src=' ${(file.response && file.response.url) || file.url}'/>`,  {
                      dangerouslyUseHTMLString: true,
                      showConfirmButton: false,
                      closeOnClickModal: true,
                      customClass: 'showPic'
                    });
                }"
                :on-success="(response, file, fileList) => {
                   inputForm.addEnclosure = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                }"
                :on-remove="(file, fileList) => {
                  $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                    $message.success(data.msg)
                  })
                  inputForm.addEnclosure = fileList.map(item => item.url).join('|')
                }"
                :before-remove="(file, fileList) => {
                  return $confirm(`确定删除 ${file.name}？`)
                }"
                multiple
                :limit="1"
                :on-exceed="(files, fileList) =>{
                  $message.warning(`当前限制选择 1 个图片，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个图片`)
                }"
                :file-list="picArra">
                <i class="el-icon-plus"></i>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="">
              </el-dialog>
          </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" v-if="method === 'add'" type="primary" @click="continueDoSubmit()">继续添加</el-button>
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" v-if="method !== 'view'" type="primary" @click="doSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        picArra: [], // 图片存储
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false,
        title: '',
        method: '',
        visible: false,
        oldInputForm: '',
        inputForm: {
          id: '',
          addReason: '',
          addTime: '',
          addPoint: '',
          addState: '',
          addEnclosure: '',
          approver: ''
        }
      }
    },
    components: {
    },
    methods: {
      init (method, obj) {
        this.method = method
        if (method === 'add') {
          this.title = `新建学生副表_德育加分`
        } else if (method === 'edit') {
          this.title = '修改学生副表_德育加分'
        } else if (method === 'view') {
          this.title = '查看学生副表_德育加分'
        }
        this.picArra = []
        this.visible = true
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = ''
          this.oldInputForm = ''
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/moredumain/studentMoreduMain/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.basicdataRepair)
              this.inputForm.addEnclosure.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.picArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
            this.oldInputForm = obj
            this.inputForm = JSON.parse(JSON.stringify(obj))
          }
        })
      },
   // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.$emit('addRow', this.oldInputForm, JSON.parse(JSON.stringify(this.inputForm)))
            this.visible = false
          }
        })
      },
      // 查看图片
      handlePictureCardPreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      continueDoSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.$emit('addRow', this.oldInputForm, JSON.parse(JSON.stringify(this.inputForm)))
            this.$refs['inputForm'].resetFields()
          }
        })
      }
    }
  }
</script>
